(function () {
RPC = function() {
	this.name = "rpc";
	this.subscriber_logged_in = new Array();
	this.subscriber_disconnected = new Array();
	this.subscriber_start_game = new Array();
	this.subscriber_end_game = new Array();
	return this;
}
RPC.prototype = {
	
	connected : function(result) {
		console.log("Connected!");
		console.log(this.name);
		var msg = {"method": "login", "arguments" : {"username" : this.username, "version" : "0.1"}};
		this.send(msg);
	},
	
	disconnect : function() {
		this.client.disconnect();
	},
	
	receive : function(result) {

		var data = JSON.parse(result.data);
		console.log("rpc - receive", typeof data);
		console.log("rpc - method", data["method"]);
		if(data["method"] === undefined || data["arguments"] === undefined) {
			console.log("rpc - invalid data!")
			return;
		}
		
		if(typeof this[data["method"]] === 'function') {
			this[data["method"]].bind(this).apply({}, data["arguments"]);
		} else {
			alert("command not found!");
		}
	},
	
	connect : function(ip, port, username) {
		this.username = username;
		this.client = new Client();
		this.client.subscribe_onopen(this.connected.bind(this));
		this.client.subscribe_onmessage(this.receive.bind(this));
		this.client.subscribe_onclose(this.disconnected.bind(this));
		this.client.connect(ip, port);
	},
	subscribe_end_game : function(callback) {
		this.subscriber_end_game.push(callback);
	},
	unsubscribe_end_game : function(callback) {
		this.subscriber_end_game.splice( $.inArray(callback, this.subscriber_end_game), 1 );
	},
	subscribe_disconnected : function(callback) {
		this.subscriber_disconnected.push(callback);
	},
	unsubscribe_disconnected : function(callback) {
		this.subscriber_disconnected.splice( $.inArray(callback, this.subscriber_disconnected), 1 );
	},
	disconnected : function(event) {
		for (var i = 0; i < this.subscriber_disconnected.length; i++) {
			this.subscriber_disconnected[i]();
		}		
	},
	
	on_error : function(msg) {
		alert(msg);
	},
	
	logged_in : function() {
		for (var i = 0; i < this.subscriber_logged_in.length; i++) {
			this.subscriber_logged_in[i]();
		}
	},
	
	subscribe_logged_in : function(callback) {
		this.subscriber_logged_in.push(callback);
	},
	
	send : function(data) {
		console.log("rpc sendet:", data)
		this.client.send(data);
	},
	send_shake_event : function() {
		var msg = {'method' : 'shake_event', 'arguments' : {}};
		this.client.send(msg);
	},
	
	send_swipe_event : function(direction) {
		// type: left, right, down, up
		var msg = {'method' : 'swipe_event', 'arguments' : {'direction': direction}};
		this.client.send(msg);
	},
	
	send_shovel_event : function() {
		var msg = {'method' : 'shovel_event', 'arguments' : {}};
		this.client.send(msg);
	},
	
	subscribe_start_game : function(callback) {
		this.subscriber_start_game.push(callback);
	},
	unsubscribe_start_game : function(callback) {
		this.subscriber_start_game.splice( $.inArray(callback, this.subscriber_start_game), 1 );
	},
	
	start_game : function(game_name, is_background, params) {
		for (var i = 0; i < this.subscriber_start_game.length; i++) {
			this.subscriber_start_game[i](game_name, is_background, params);
		}
	},
	
	send_ready : function() {
		var msg = {'method' : 'ready', 'arguments' : {}};
		this.client.send(msg);
	},
	send_button_event : function() {
		var msg = {'method' : 'button_event', 'arguments' : {}};
		this.client.send(msg);
	},
	
	send_is_upside_down : function(is_upside_down) {
		var msg = {'method' : 'is_upside_down', 'arguments' : {'is_upside_down' : is_upside_down }};
		this.client.send(msg);	
	},
	send_qrcode_event : function(text, format, cancelled) {
		var msg = {'method' : 'qrcode_event', 'arguments' : {'text' : text, 'format' : format, 'cancelled' : cancelled }};
		this.client.send(msg);
	}, 
	end_game : function() {
		console.log("Game ended");
		for (var i = 0; i < this.subscriber_end_game.length; i++) {
			this.subscriber_end_game[i]();
		}
	},
	send_image_quiz_event : function(image_name) {
		var msg = {'method' : 'image_quiz_event', 'arguments' : {'image_name' : image_name}};
		this.client.send(msg);
	}
	
};

})();